﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GArphics
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();

            cbxRandomizeColors.Checked = Properties.Settings.Default.RandomizeColors;
            cbdRandomizeCoordinates.Checked = Properties.Settings.Default.RandomizeCoordinates;
            tbxColorModifierProbability.Text = Properties.Settings.Default.ColorChannelModifierProbability.ToString();
            tbxConstantValueProbability.Text = Properties.Settings.Default.ConstantValueProbability.ToString();
            tbxFailValue.Text = Properties.Settings.Default.FailValue.ToString();
            tbxMaxTreeDepth.Text = Properties.Settings.Default.MaxTreeDepth.ToString();
            tbxMinTreeDepth.Text = Properties.Settings.Default.MinTreeDepth.ToString();
            tbxOperandXProbability.Text = Properties.Settings.Default.OperandXProbability.ToString();
            tbxOperatorProbability.Text = Properties.Settings.Default.OperatorProbability.ToString();
            tbxThreads.Text = Properties.Settings.Default.NumberOfThreads.ToString();
            tbxRandomizeXfrom.Text = (-Properties.Settings.Default.RandomizeXSubstraction).ToString();
            tbxRandomizeXto.Text = (Properties.Settings.Default.RandomizeXMultiplier - Properties.Settings.Default.RandomizeXSubstraction).ToString();
            tbxRandomizeYfrom.Text = (-Properties.Settings.Default.RandomizeYSubstraction).ToString();
            tbxRandomizeYto.Text = (Properties.Settings.Default.RandomizeYMultiplier - Properties.Settings.Default.RandomizeYSubstraction).ToString();
            tbxRandomizeConstantFrom.Text = (-Properties.Settings.Default.ConstantValueSubstraction).ToString();
            tbxRandomizeConstantTo.Text = (Properties.Settings.Default.ConstantValueMultiplier - Properties.Settings.Default.ConstantValueSubstraction).ToString();

            tbxMutateAmountMin.Text = Properties.Settings.Default.MutateAmountMin.ToString();
            tbxMutateAmountMax.Text = Properties.Settings.Default.MutateAmountMax.ToString();
            tbxMutateNewBranchProbability.Text = Properties.Settings.Default.MutateNewBranchProbability.ToString();
            tbxMutateChangeOperatorOperandProbability.Text = Properties.Settings.Default.MutateChangeOperatorOperandProbability.ToString();
            tbxMutateChangeColorProbability.Text = Properties.Settings.Default.MutateChangeColorProbability.ToString();
            tbxMutateChangeCoordinateProbability.Text = Properties.Settings.Default.MutateChangeCoordinateProbability.ToString();
            tbxAnimationStepMax.Text = Properties.Settings.Default.AnimationStepMax.ToString();
        }

        private void Settings_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                Properties.Settings.Default.RandomizeColors = cbxRandomizeColors.Checked;
                Properties.Settings.Default.RandomizeCoordinates = cbdRandomizeCoordinates.Checked;
                Properties.Settings.Default.ColorChannelModifierProbability = Convert.ToDouble(tbxColorModifierProbability.Text);
                Properties.Settings.Default.ConstantValueProbability = Convert.ToDouble(tbxConstantValueProbability.Text);
                Properties.Settings.Default.FailValue = Convert.ToDouble(tbxFailValue.Text);
                Properties.Settings.Default.MaxTreeDepth = Convert.ToInt32(tbxMaxTreeDepth.Text);
                Properties.Settings.Default.MinTreeDepth = Convert.ToInt32(tbxMinTreeDepth.Text);
                Properties.Settings.Default.OperandXProbability = Convert.ToDouble(tbxOperandXProbability.Text);
                Properties.Settings.Default.OperatorProbability = Convert.ToDouble(tbxOperatorProbability.Text);
                Properties.Settings.Default.NumberOfThreads = Convert.ToInt32(tbxThreads.Text);


                int iRandomizeXfrom = Convert.ToInt32(tbxRandomizeXfrom.Text);
                int iRandomizeXto = Convert.ToInt32(tbxRandomizeXto.Text);
                int iRandomizeYfrom = Convert.ToInt32(tbxRandomizeYfrom.Text);
                int iRandomizeYto = Convert.ToInt32(tbxRandomizeYto.Text);
                int iRandomizeConstantFrom = Convert.ToInt32(tbxRandomizeConstantFrom.Text);
                int iRandomizeConstantTo = Convert.ToInt32(tbxRandomizeConstantTo.Text);

                Properties.Settings.Default.RandomizeXSubstraction = -iRandomizeXfrom;
                Properties.Settings.Default.RandomizeXMultiplier = -iRandomizeXfrom + iRandomizeXto;

                Properties.Settings.Default.RandomizeYSubstraction = -iRandomizeYfrom;
                Properties.Settings.Default.RandomizeYMultiplier = -iRandomizeYfrom + iRandomizeYto;

                Properties.Settings.Default.ConstantValueSubstraction = -iRandomizeConstantFrom;
                Properties.Settings.Default.ConstantValueMultiplier = -iRandomizeConstantFrom + iRandomizeConstantTo;


                Properties.Settings.Default.MutateAmountMin = Convert.ToInt32(tbxMutateAmountMin.Text);
                Properties.Settings.Default.MutateAmountMax = Convert.ToInt32(tbxMutateAmountMax.Text);
                Properties.Settings.Default.MutateNewBranchProbability = Convert.ToDouble(tbxMutateNewBranchProbability.Text);
                Properties.Settings.Default.MutateChangeOperatorOperandProbability = Convert.ToDouble(tbxMutateChangeOperatorOperandProbability.Text);
                Properties.Settings.Default.MutateChangeColorProbability = Convert.ToDouble(tbxMutateChangeColorProbability.Text);
                Properties.Settings.Default.MutateChangeCoordinateProbability = Convert.ToDouble(tbxMutateChangeCoordinateProbability.Text);

                Properties.Settings.Default.AnimationStepMax = Convert.ToInt32(tbxAnimationStepMax.Text);

                Properties.Settings.Default.Save();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Saving settings failed.\n" + exc.Message);
            }
        }
    }
}
